package com.mktneutral.etf;

import com.ning.http.client.AsyncHttpClient;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created with IntelliJ IDEA.
 * User: JVigil0515c
 * Date: 8/15/12
 * Time: 11:20 PM
 * To change this template use File | Settings | File Templates.
 */
public class AsyncHttpTest {
    private static final List<ETF> etfs = new ArrayList<ETF>();
    public static Logger logger;
    
	
    /**
     * @param args
     */
    /**
     * @param args
     */
    public static void main(String[] args) {
        try {
        	 logger = LoggerFactory.getLogger(AsyncHttpTest.class);
             getETFList();	
        } catch ( IOException ioe ) { ioe.printStackTrace(); }
        
        AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
        String prefix = "http://ichart.finance.yahoo.com/table.csv?s=";
        String suffix = "&a=08&b=1&c=2010&d=07&e=5&f=2012&g=v&ignore=.csv";

        List<Future<?>> futuresList = new ArrayList<Future<?>>();
        
        for (final ETF etf : etfs) {
            try {
                futuresList.add( asyncHttpClient.prepareGet(prefix + etf.getTicker() + suffix).execute(new MyCompletionHandler(etf)) );
                Thread.sleep(100);
            } catch (IOException ioe) {
                ioe.printStackTrace();
            }
            catch ( InterruptedException ie ) {
            	ie.printStackTrace();
            }
            catch ( Exception e ) {
            	e.printStackTrace();
            }
        }
        
        for ( Future<?> f : futuresList ) {
        	try {
        	    f.get(2000, TimeUnit.MILLISECONDS);
        	    logger.debug( "getting f" );
        	} catch ( ExecutionException ee ) { ee.printStackTrace(); }
        	catch ( InterruptedException ie ) { ie.printStackTrace(); }
        	catch ( TimeoutException te ) { te.printStackTrace(); }
        }
        
        printSortedETFs(); 
    }
 
    public static void getETFList() throws IOException {
    	 URL url = new URL("http://www.masterdata.com/HelpFiles/ETF_List_Downloads/AllETFs.csv");
    	 BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(url.openStream()));
    	
    	 String line;
    	 while( (line = bufferedReader.readLine()) != null ) {
    		String[] cols = line.split(",");
    		etfs.add(new ETF(cols[1].trim()));
    	 }
    	 
    	 bufferedReader.close();
    }
    
    public static void printSortedETFs() {
    	Collections.sort(etfs);
    	
    	for ( ETF etf : etfs ) {
    		logger.info( etf.getTicker() + ", dividend = " + etf.getDividendYield() );
    	}
    }
}
